package student.aistar.mapper;

import org.apache.ibatis.annotations.*;
import student.aistar.model.entity.Cart;

import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: qinFeng
 * @date: 2021/9/27 9:21
 */
@Mapper
public interface CartMapper {
    /**
     * 根据用户Id查询购物车
     * @param userId
     * @return
     */
    @Select("select * from cart where user_id = #{userId}")
    List<Cart> selectCartByUserId(Integer userId);


    /**
     * 根据ID删除购物车
     */
    @Delete("delete from cart where cart_id = #{Id}")
    void deleteCartById(Integer id);


    @Update("update cart set num=#{num}, totalprice=#{price}*num where user_id = #{userId} and product_id = #{productId} and paraName = #{paraName}")
    void updateCart(double price, Integer num, Integer userId, Integer productId, String paraName);


    /**
     * 根据产品Id和用户Id查询数据
     * @param productId
     * @param userId
     * @return
     */
    @Select("select * from cart where product_id = #{productId} and user_id = #{userId}")
    Cart select(Integer productId, Integer userId);


    /**
     * 插入购物车
     * @param cart
     */
    @Insert("insert into cart values(null, #{userId}, #{productId}, #{num}, 0, 1, #{paraPrice},#{paraName},#{paraPrice})")
    void addCart(Cart cart);




    /**
     * 根据产品Id和用户Id, 并且根据购物车中的名称判断是否相等, 相等的话更新一个查询数据
     * @param productId
     * @param userId
     * @return
     */
    @Select("select * from cart where user_id = #{userId} and product_id = #{productId} and paraName=#{paraName}")
    Cart selectCart(Integer userId, Integer productId, String paraName);


    /**
     * 根据购物车的ID, 查询购物车的数据
     * @param id
     * @return
     */
    @Select("select * from cart where cart_id = #{id}")
    Cart selectCartById(Integer id);
}
